<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.singlePerson" placeholder="制单人姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="filters.orderNum" placeholder="单号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="filters.needDayStart"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="需求开始日期"
          ></el-date-picker>-
          <el-date-picker
            v-model="filters.needDayStop"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="需求结束日期"
          ></el-date-picker>
        </el-form-item>
        <!-- <el-form-item>
          <el-input v-model="filters.productName" placeholder="产品名称"></el-input>
        </el-form-item>-->
        <el-form-item>
          <el-input v-model="filters.custname" placeholder="供应商名称"></el-input>
        </el-form-item>
        <el-form-item label="单据状态：">
          <el-select v-model="filters.type" placeholder="单据状态" @change="changeFile(1)">
            <el-option v-for="item in billState" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="待我审批：">
          <el-select v-model="filters.nowUserAudit" placeholder="是否待自己审批" @change="changeFile(2)">
            <el-option v-for="item in isMeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getList">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-button type="primary" v-on:click="delList">批量删除</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="approvalList">批量审批</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="withDrawList">批量撤回</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="closeList">批量关闭</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <!--列表-->
    <el-table
      :data="list"
      highlight-current-row
      v-loading="listLoading"
      @selection-change="selsChange"
      style="width: 100%;"
      height="44vh"
    >
      <el-table-column type="selection" width="60" align="center"></el-table-column>
      <el-table-column type="index" label="序号" width="60" align="center"></el-table-column>
      <el-table-column prop="orderNum" label="请购单号"></el-table-column>
      <el-table-column prop="psnname" label="请购人名"></el-table-column>
      <el-table-column prop="singlePerson" label="制单人"></el-table-column>
      <el-table-column prop="type" label="订单状态" :formatter="getBillState"></el-table-column>
      <el-table-column prop="unitname" label="公司名"></el-table-column>
      <!-- <el-table-column prop="custname" label="供应商"></el-table-column> -->
      <el-table-column prop="needDay" label="需求日期"></el-table-column>
      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">详情</el-button>
          <el-button type="danger" size="small" @click="deleteItem(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination
        layout="prev, pager, next"
        @current-change="handleCurrentChange"
        :page-size="this.page.pageSize"
        :total="total"
        style="float:right;"
      ></el-pagination>
    </el-col>
    <!--编辑界面-->
    <el-dialog title="修改请购单" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <purchForm @hideTotas="hideTotas"></purchForm>
    </el-dialog>
    <!--新增界面-->
    <el-dialog title="新增请购单" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <purchForm @hideTotas="hideTotas"></purchForm>
    </el-dialog>
  </section>
</template>

<script>
import util from "../../common/js/util";
import purchForm from "./components/purchForm";
//import NProgress from 'nprogress'
import {
  wlcSysAllList,
  wlcSysAllRemove,
  wlcSysAllAuditSubmit,
  wlcSysAllWithdrawTicket,
  wlcSysAllCloseTicket
} from "../../api/api";

export default {
  components: {
    purchForm
  },
  data() {
    return {
      filters: {
        singlePerson: "",
        orderNum: "",
        needDayStart: "",
        needDayStop: "",
        custname: "",
        type: 2,
        nowUserAudit: "0"
      },
      list: [],
      page: {
        page: 1,
        pageSize: 10
      },
      total: 0,

      listLoading: false,
      sels: [], //列表选中列

      editFormVisible: false, //编辑界面是否显示
      editLoading: false,
      editFormRules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }]
      },
      //编辑界面数据
      editForm: {
        id: 0,
        name: "",
        sex: -1,
        age: 0,
        birth: "",
        addr: ""
      },

      addFormVisible: false, //新增界面是否显示
      addLoading: false,
      addFormRules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }]
      },
      //新增界面数据
      addForm: {
        name: "",
        sex: -1,
        age: 0,
        birth: "",
        addr: ""
      },
      //单据状态
      billState: [
        {
          name: "草稿",
          id: 0
        },
        {
          name: "保存",
          id: 1
        },
        {
          name: "待审批",
          id: 2
        },
        {
          name: "废除",
          id: 3
        },
        {
          name: "不通过",
          id: 4
        },
        {
          name: "审核通过",
          id: 5
        },
        {
          name: "打开状态",
          id: 6
        },
        {
          name: "关闭状态",
          id: 7
        },
        {
          name: "撤回状态",
          id: 8
        },
        {
          name:'已处理',
          id:9
        }
      ],
      //是否自己审批的
      isMeList: [
        {
          name: "待我审批",
          id: "1"
        },
        {
          name: "全部",
          id: "0"
        }
      ]
    };
  },
  methods: {
    //改变 单据状态  或者  待自己审批的条件
    changeFile(order) {
      if (order == 1) {
        this.filters.nowUserAudit = "0";
      }
      if (order == 2) {
        this.filters.type = "";
      }
    },
    //批量关闭
    closeList() {
      this.$confirm("确认关闭?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(res => {
          let arr = [];
          for (let i = 0; i < this.sels.length; i++) {
            let obj = {
              id: this.sels[i].id
            };
            arr.push(obj);
          }
          wlcSysAllCloseTicket(arr).then(res => {
            this.$message({
              type: "success",
              message: "关闭成功!"
            });
            this.getList();
          });
        })
        .catch(() => {});
    },
    //批量撤回
    withDrawList() {
      this.$confirm("确认撤回该请购单?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let arr = [];
          for (let i = 0; i < this.sels.length; i++) {
            let obj = {
              id: this.sels[i].id,
              buyType: this.sels[i].buyType,
              deptnameId: this.sels[i].deptnameId
            };
            arr.push(obj);
          }
          wlcSysAllWithdrawTicket(arr).then(res => {
            this.$message({
              type: "success",
              message: "撤回成功!"
            });
            this.getList();
          });
        })
        .catch(() => {});
    },
    //批量审批
    approvalList() {
      let reason = "";
      let arr = [];
      this.$prompt("请输入理由(通过可不填写)", "是否审批通过", {
        confirmButtonText: "通过",
        cancelButtonText: "不通过",
        type: "warning",
        distinguishCancelAndClose: true,
        beforeClose: (action, instance, done) => {
          reason = instance.inputValue || "";
          if (action == "cancel") {
            if (instance.inputValue) {
              done();
            } else {
              this.$message({
                type: "warning",
                message: "请输入拒绝理由"
              });
            }
          } else {
            done();
          }
        }
      })
        .then(val => {
          for (let i = 0; i < this.sels.length; i++) {
            let obj = {
              id: this.sels[i].id,
              buyType: this.sels[i].buyType,
              deptnameId: this.sels[i].deptnameId,
              pkInvbasdoc: this.sels[i].pkInvbasdoc,
              reason: reason,
              type: 2
            };
            arr.push(obj);
          }
          wlcSysAllAuditSubmit(arr)
            .then(res => {
              this.$message({
                type: "success",
                message: "审批成功!"
              });
            })
            .catch(err => {});
        })
        .catch(action => {
          if (action == "cancel") {
            for (let i = 0; i < this.sels.length; i++) {
              let obj = {
                id: this.sels[i].id,
                buyType: this.sels[i].buyType,
                deptnameId: this.sels[i].deptnameId,
                pkInvbasdoc: this.sels[i].pkInvbasdoc,
                reason: reason,
                type: 4
              };
              arr.push(obj);
            }
            wlcSysAllAuditSubmit(arr).then(res => {
              this.$message({
                type: "warning",
                message: "审批不通过!"
              });
            });
          }
          // message: action === 'cancel'
        });
    },
    //批量删除
    delList() {
      let arr = [];
      for (let i = 0; i < this.sels.length; i++) {
        let obj = {
          id: this.sels[i].id
        };
        arr.push(obj);
      }
      this.handleDel(arr);
    },
    //单独删除
    deleteItem(item) {
      let arr = [];
      let obj = {
        id: item.id
      };
      arr.push(obj);
      this.handleDel(arr);
    },
    //获取订单状态
    getBillState(row) {
      for (let i = 0; i < this.billState.length; i++) {
        if (row.type == this.billState[i].id) {
          return this.billState[i].name;
        }
      }
    },
    //点击打印
    Printing() {},
    //隐藏弹窗
    hideTotas() {
      this.addFormVisible = false;
      this.editFormVisible = false;
    },
    handleCurrentChange(val) {
      this.page.page = val;
      this.getList();
    },

    //删除
    handleDel: function(arr) {
      this.$confirm("确认删除该记录吗?", "提示", {
        type: "warning"
      })
        .then(() => {
          this.listLoading = true;
          wlcSysAllRemove(arr).then(res => {
            this.listLoading = false;
            this.$message({
              message: "删除成功",
              type: "success"
            });
            this.getList();
          });
        })
        .catch(() => {});
    },
    //显示编辑界面
    handleEdit: function(index, row) {
      this.$router.push({
        path: "purchForm",
        query: {
          id: row.id,
          buyType: row.buyType
        }
      });
      // this.editFormVisible = true;
      // this.editForm = Object.assign({}, row);
    },
    //显示新增界面
    handleAdd: function() {
      this.$router.push({ path: "purchForm" });
      // this.addFormVisible = true;
      // this.addForm = {
      //   name: "",
      //   sex: -1,
      //   age: 0,
      //   birth: "",
      //   addr: ""
      // };
    },
    //编辑
    editSubmit: function() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            this.editLoading = true;
            //NProgress.start();
            let para = Object.assign({}, this.editForm);
            para.birth =
              !para.birth || para.birth == ""
                ? ""
                : util.formatDate.format(new Date(para.birth), "yyyy-MM-dd");
            editUser(para).then(res => {
              this.editLoading = false;
              //NProgress.done();
              this.$message({
                message: "提交成功",
                type: "success"
              });
              this.$refs["editForm"].resetFields();
              this.editFormVisible = false;
              this.getList();
            });
          });
        }
      });
    },
    selsChange: function(sels) {
      this.sels = sels;
    },

    //获取列表
    getList() {
      var para = this.filters;
      para.page = this.page;
      this.listLoading = true;
      wlcSysAllList(para).then(res => {
        console.log(res);
        this.listLoading = false;
        this.total = res.total;
        this.list = res.records;
      });
    }
    //计算最高价格，最低价格，平均价格接口
  },
  mounted() {
    this.getList();
  }
};
</script>
<style lang="less" scoped>
</style>
